package cn.group.hotel2.service.impl;

import cn.group.hotel2.ex.ServiceException;
import cn.group.hotel2.mapper.TypeMapper;
import cn.group.hotel2.pojo.dto.TypeAddNewDTO;
import cn.group.hotel2.pojo.dto.TypeUpdateDTO;
import cn.group.hotel2.pojo.entity.Type;
import cn.group.hotel2.pojo.vo.TypeListItemVO;
import cn.group.hotel2.pojo.vo.TypeStandardVO;
import cn.group.hotel2.service.ITypeService;
import cn.group.hotel2.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class TypeServiceImpl implements ITypeService {

    @Autowired
    private TypeMapper typeMapper;

    @Override
    public List<TypeListItemVO> list(Integer current) {
        return typeMapper.list(current);
    }

    @Override
    public List<TypeListItemVO> getAll() {
        return typeMapper.getAll();
    }


    @Override
    public void addNew(TypeAddNewDTO typeAddNewDTO) {
        TypeStandardVO typeStandardVO = typeMapper.getStandardByTypeName(typeAddNewDTO.getName());
        if (typeStandardVO!=null){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"分类已存在");
        }
        Type type = new Type();
        BeanUtils.copyProperties(typeAddNewDTO,type);
        typeMapper.insert(type);
    }

    @Override
    public void updateById(Long id, TypeUpdateDTO typeUpdateDTO) {
        TypeStandardVO typeStandardVO= typeMapper.getStandardByTypeName(typeUpdateDTO.getName());
        if (typeStandardVO!=null){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"分类已存在");
        }
        Type type = new Type();
        BeanUtils.copyProperties(typeUpdateDTO,type);
        typeMapper.update(type);
    }

    @Override
    public TypeStandardVO getUpdateStandardById(Long id) {
        return typeMapper.getStandardById(id);
    }

    @Override
    public TypeStandardVO getStandardByName(String name) {
        return typeMapper.getStandardByTypeName(name);
    }

    @Override
    public void deleteById(Long id) {
        typeMapper.deleteById(id);
    }

    @Override
    public int getCount() {
        return typeMapper.count();
    }


}
